﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;

namespace FrogEngine
{
	public class TestScreen : GameScreen
	{
		public TestScreen(IEngine engine, string state)
			: base(engine)
		{
			batch = engine.SpriteBatch;
			
			Services.Add(engine.GetService<GameContentService>());

			foreach (var service in Services)
				service.Subscribe(this);

			TransitionOnTime = TimeSpan.FromSeconds(2);
			TransitionOffTime = TimeSpan.FromSeconds(1);
			StateName = state;
		}

		private SpriteBatch batch;
		private Texture2D spriteTexture;

		/// <summary>
		/// List of asset names this entity uses... be sure to actually
		/// load all of the assets in this list in LoadContent.
		/// </summary>
		/// <returns></returns>
		public override IEnumerable<string> AssetNames()
		{
			var list = new List<string>();
			list.Add("pic");
			return list;
		}

		/// <summary>
		/// Not meant to be called directly... subscribe this entity to the
		/// GameContentService.  It will ensure that this method is called.
		/// </summary>
		public override void LoadContent()
		{
			spriteTexture = ContentManager.Load<Texture2D>("pic");
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
		}

		public override void Render(GameTime gameTime)
		{
			batch.Begin();
			float c = (float)TransitionProgress;
			batch.Draw(spriteTexture, new Rectangle((int)p.X, (int)p.Y, 400, 400), new Color(c, c, c, c));
			batch.End();
			base.Render(gameTime);
		}

		private const float pixelsPerSec = 400;
		private Vector2 p = new Vector2(0f, 0f);
		public override void HandleInput(GameTime gameTime, PlayerIndex index, GamePadState currentState, GamePadState previousState)
		{
			
		}

		public override void HandleInput(GameTime gameTime, PlayerIndex index, KeyboardState currentState, KeyboardState previousState)
		{
			if (currentState.IsKeyDown(Keys.K) && previousState.IsKeyUp(Keys.K))
				this.IsAlive = false;
			if (currentState.IsKeyDown(Keys.Right))
				p.X += pixelsPerSec * (float)gameTime.ElapsedGameTime.TotalSeconds;
			if (currentState.IsKeyDown(Keys.Left))
				p.X -= pixelsPerSec * (float)gameTime.ElapsedGameTime.TotalSeconds;
			if (currentState.IsKeyDown(Keys.Up))
				p.Y -= pixelsPerSec * (float)gameTime.ElapsedGameTime.TotalSeconds;
			if (currentState.IsKeyDown(Keys.Down))
				p.Y += pixelsPerSec * (float)gameTime.ElapsedGameTime.TotalSeconds;
			if (currentState.IsKeyDown(Keys.A) && previousState.IsKeyUp(Keys.A))
				foreach (var a in Transitions)
					if (a.Key == "state2")
					{
						this.ScreenManager.Transition(a.Value as IGameScreen);
						break;
					}
		}
	}
}
